/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.core.expand.spring;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.common.util.SystemStatu;
import cn.com.ksplatform.common.util.SystemUtil;
import cn.com.ksplatform.core.context.PlatformContext;
import cn.com.ksplatform.core.hsqldb.DbUtil;
import cn.com.ksplatform.install.model.InstallInfo;
import cn.com.ksplatform.install.service.DBType;
import cn.com.ksplatform.install.util.DbHelp;

/**
 * 资源加载器
 * 
 * @author Simon
 * 
 */
public class PropertyPlaceholderConfigurer extends
		org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
	/**
	 * 初始化任务 1.完成系统配置的动态载入
	 */
	public PropertyPlaceholderConfigurer() {
		InstallInfo info = new InstallInfo();
		if (PlatformContext.SYSTEMSTATU.equals(SystemStatu.INSTALLED)) {
			Log.info("加载系统安装信息。");
			try {
				Log.info("读取内部配置文件。");
				info = SystemUtil.getSysConfigModel();
				if(info == null){
					Log.info("无法读取内部文件。。 读取容器配置");
					info = SystemUtil
							.getSysConfigModel(PlatformContext.CONTEXT_PATH);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			Log.info("未检测到系统按照信息。  加载系统文件数据库");
			//启动本地数据库
			DbUtil.runLocalDb(9002);
			info.setDataBaseType(DBType.HSQLDB.getValue());
			info.setDataBaseHost("127.0.0.1");
			info.setDataBaseName("ksPlatform");
			info.setDataBaseUserName("sa");
			info.setDataBasePassWord("");
			info.setDataBasePort(9002);
			DbHelp.setUrl(info);
			PlatformContext.SYSTEMSTATU = SystemStatu.INSTALLED;
		}

		Properties p = new Properties();
		p.setProperty("dataBaseUrl", info.getDataBaseUrl());
		p.setProperty("dataBaseUserName", info.getDataBaseUserName());
		p.setProperty("dataBasePassWord", info.getDataBasePassWord());
		p.setProperty("hibernateProperties",info.hibernateProperties);
		super.setProperties(p);
	}
}
